<?php

namespace App\Imports;

use App\Models\Client;
use Carbon\Carbon;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\Request;
use Maatwebsite\Excel\Concerns\ToModel;

class SeaImport implements ToModel
{

    // todo
    public function model(array $row)
    {
        $referer = Request::header('Referer');
        $true_client = 0; // 0 线索  1客户
        $in_sea = 0;        // 1 在公海 2 不在公海 默认0
        if(strstr('/clue',$referer)){
            #  是线索的话，什么也不用做
        }else if(strstr('/client',$referer)){
            #  客户的话，要求
            $true_client = 1;
            $in_sea = 0;
        }else if(strstr('/sea',$referer)){
            # 公海是客户的公海，不包括线索
            $true_client = 1;
            $in_sea = 1;
        }
        $mobile = $row[1];
        if(!empty($mobile) && is_numeric($mobile)){
//            if(! Client::query()->where('mobile',$mobile)->exists()){
                $data = [
                    'name' => $row[0],
                    'mobile' => $mobile,
                    'sex' => $row[2],
                    'marriage' => $row[3],
                    'email' => $row[4],
                    'education' => $row[5],
                    'age' => $row[6],
                    'china_id' => $row[7],
                    'china_id_valid' => $row[8],
                    'income' => $row[9],
                    'family_in' => $row[10],
                    'family_out' => $row[11],
                    'shebao' => $row[12],
                    'shebao_in' => $row[13],
                    'gongjijin' => $row[14],
                    'gongjijin_in' => $row[15],
                    'company' => $row[16],
                    //'company_address' => $row[17],
                    'company_type' => $row[17],
                    'company_belong' => $row[18],
                    'company_post' => $row[19],
                    'debt' => $row[20],
                    'monthly_debt' => $row[21],
                    'note' => $row[22],
                    'true_client'=>$true_client,
                    'in_sea'=>$in_sea,
                    'employee_id'=>Admin::user()->id,
                    'created_at'=>Carbon::now()->toDateTimeString(),
                    ];
                return new Client($data);
//            }
        }
        return NULL;
    }
}
